package com.huaguoguo.cyg.service.tl.buss;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.additional.query.impl.LambdaQueryChainWrapper;
import com.huaguoguo.cyg.annotation.ExecTime;
import com.huaguoguo.cyg.entity.tl.dto.TlServerDTO;
import com.huaguoguo.cyg.entity.tl.to.Gang;
import com.huaguoguo.cyg.service.tl.data.ITlServerService;
import com.huaguoguo.cyg.spider.listener.FailSpiderListener;
import com.huaguoguo.cyg.spider.pipeline.TLRankPipeline;
import com.huaguoguo.cyg.spider.processor.tl.TLGangRankProcessor;
import com.huaguoguo.cyg.spider.processor.tl.TLPalyerRankProcessor;
import com.huaguoguo.cyg.spider.processor.tl.TLTrumpetRankProcessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.SpiderListener;

import java.util.List;

/**
 * 类说明： 天龙排行榜服务接口<br/>
 * author:huaguoguo
 * Date: 2020/2/23
 * Time: 13:22
 */
@Service
public class TLRankService {

    @Autowired
    private TLRankPipeline rankPipeline;
    @Autowired
    private ITlServerService tlServerService;
    @Autowired
    private FailSpiderListener failSpiderListener;

    /**
     * 爬取所有区的帮派排行榜
     */
    @ExecTime
    public void spiderGangRankAll(){
        QueryWrapper<TlServerDTO> queryWrapper = new QueryWrapper<TlServerDTO>()
                .select("area_id", "GROUP_CONCAT(area_name) as areaName")
                .groupBy("area_id");
        List<TlServerDTO> tlServerDTOS = tlServerService.list(queryWrapper);
        tlServerDTOS.parallelStream().forEach(tlServerDTO -> spiderGangRank(tlServerDTO.getAreaId()));
    }

    /**
     * 爬取帮派排行榜
     * @param areaId 区id
     * @return
     */
    public void spiderGangRank(Integer areaId){
        TLGangRankProcessor processor = new TLGangRankProcessor();
        processor.addData("areaId", areaId);
        Spider.create(processor)
                .addUrl(processor.buildRequestUrl())
                .addPipeline(rankPipeline)
                .setSpiderListeners(CollectionUtil.list(false,failSpiderListener))
                .run();
    }

    /**
     * 爬取所有区的玩家战力排行榜
     */
    @ExecTime
    public void spiderPlayerRankAll(){
        QueryWrapper<TlServerDTO> queryWrapper = new QueryWrapper<TlServerDTO>()
                .select("area_id", "GROUP_CONCAT(area_name) as areaName")
                .groupBy("area_id");
        List<TlServerDTO> tlServerDTOS = tlServerService.list(queryWrapper);
        tlServerDTOS.parallelStream().forEach(tlServerDTO -> spiderPlayerRank(tlServerDTO.getAreaId()));
    }

    /**
     * 爬取指定区的玩家战力排行榜
     * @param areaId 区id
     * @return
     */
    public void spiderPlayerRank(Integer areaId){
        TLPalyerRankProcessor processor = new TLPalyerRankProcessor();
        processor.addData("areaId", areaId);
        Spider.create(processor)
                .addUrl(processor.buildRequestUrl())
                .addPipeline(rankPipeline)
                .run();
    }

    /**
     * 爬取指定区的小喇叭排行榜
     * @param areaId 区id
     * @return
     */
    public void spiderTrumpetRank(Integer areaId){
        TLTrumpetRankProcessor processor = new TLTrumpetRankProcessor();
        processor.addData("areaId", areaId);
        Spider.create(processor)
                .addUrl(processor.buildRequestUrl())
                .addPipeline(rankPipeline)
                .run();
    }

    /**
     * 爬取所有区的小喇叭排行榜
     * @return
     */
    @ExecTime
    public void spiderTrumpetRankAll(){
        QueryWrapper<TlServerDTO> queryWrapper = new QueryWrapper<TlServerDTO>()
                .select("area_id", "GROUP_CONCAT(area_name) as areaName")
                .groupBy("area_id");
        List<TlServerDTO> tlServerDTOS = tlServerService.list(queryWrapper);
        tlServerDTOS.parallelStream().forEach(tlServerDTO -> spiderTrumpetRank(tlServerDTO.getAreaId()));
    }
}
